package com.cate.controller.user;

import com.cate.mapper.AddressBookMapper;
import com.cate.pojo.entity.AddressBook;
import com.cate.result.RestResponse;
import com.cate.service.AddressBookService;
import com.cate.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime: 2023/8/1
 * @Author: <==lhd==>
 * @Description: 用户端地址管理
 */
@RestController("UserAddressBookController")
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "用户端地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public RestResponse<Object> deleteById(@RequestParam("id") Long id){
        addressBookService.deleteById(id);
        return ResultUtil.success("");
    }

    @PutMapping
    @ApiOperation("根据id修改地址")
    public RestResponse<Object>  updateById(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return ResultUtil.success("");
    }

    @PostMapping
    @ApiOperation("新增地址")
    public RestResponse<Object> save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return ResultUtil.success("");
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public RestResponse<AddressBook> getDefaultAddress(){
        final AddressBook defaultAddress = addressBookService.getDefaultAddress();
        return ResultUtil.success(defaultAddress);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public RestResponse<Object> setDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddress(addressBook);
        return ResultUtil.success("");
    }

    @GetMapping("/list")
    @ApiOperation("查询用户所有地址")
    public RestResponse<List<AddressBook>> list(){
        final List<AddressBook> list = addressBookService.list();
        return ResultUtil.success(list);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public RestResponse<AddressBook> getById(@PathVariable("id") Long id){
        final AddressBook addressBook = addressBookService.getById(id);
        return ResultUtil.success(addressBook);
    }

}
